package com.dkm.orproject.dkmoa.service.serviceimpl;

import com.dkm.orproject.dkmoa.dao.OaRegimeTypeMapper;
import com.dkm.orproject.dkmoa.domain.OaRegimeType;
import com.dkm.orproject.dkmoa.service.OaRegimeTypeService;
import com.dkm.orproject.utils.utils.IdToobtainUtils;
import com.dkm.orproject.utils.utils.ThreeVo;
import com.dkm.orproject.utils.utils.Tools;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *  @ClassName : OaRegimeTypeServiceImpl  //类名
 *  @Description : OaRegimeTypeService的实现类 //描述
 *  @Author : zmh //作者
 *  @Date: 2020-11-12 09:28  //时间
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class OaRegimeTypeServiceImpl implements OaRegimeTypeService {

    /**
     * id生成工具类
     */
    @Resource
    IdToobtainUtils toobtainUtils;

    @Resource
    Tools tools;

    /**
     * 规则制度类型类的mapper对象
     */
    @Resource
    OaRegimeTypeMapper oaRegimeTypeMapper;

    /**
     * 根据id删除
     * @param pkRegimeTypeId 规则制度类型id
     * @return 回调函数
     */
    @Override
    public int deleteByPrimaryKey(Long pkRegimeTypeId) {
        return oaRegimeTypeMapper.deleteByPrimaryKey(pkRegimeTypeId);
    }

    /**
     * 插入规则制度类型
     * @param oaRegimeType 规章制度类型对象
     * @param personnelName 登录人名
     * @return 回调函数
     */
    @Override
    public int insertSelective(OaRegimeType oaRegimeType, String personnelName) {
        oaRegimeType.setPkRegimeTypeId(toobtainUtils.getNumberId());
        oaRegimeType.setCreatedBy(personnelName);
        return oaRegimeTypeMapper.insertSelective(oaRegimeType);
    }

    /**
     * 根据id 查询规则制度类型
     * @param pkRegimeTypeId 规则制度类型id
     * @return 规则制度类型对象
     */
    @Override
    public OaRegimeType selectByPrimaryKey(Long pkRegimeTypeId) {
        return oaRegimeTypeMapper.selectByPrimaryKey(pkRegimeTypeId);
    }

    /**
     * 更新修改规则制度类型
     * @param oaRegimeType  规则制度类型对象
     * @param personnelName 登录人名
     * @return 回调函数
     */
    @Override
    public int updateByPrimaryKeySelective(OaRegimeType oaRegimeType, String personnelName) {
        oaRegimeType.setUpdatedBy(personnelName);
        oaRegimeType.setUpdatedTime(new Date());
        return oaRegimeTypeMapper.updateByPrimaryKey(oaRegimeType);
    }


    /**
     * 规则制度类型总行数
     * @param oaRegimeType 规则制度类型对象
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return  总行数
     */
    @Override
    public Long rowCount(OaRegimeType oaRegimeType, String startTime, String endTime) {
        return oaRegimeTypeMapper.rowCount(oaRegimeType,startTime,endTime);
    }

    /**
     * 分页查询所有规则制度类型记录
     * @param oaRegimeType 规则制度类型对象
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param page 当前页数
     * @param limit 显示的行数
     * @return 查询规则制度模型结果集合
     */
    @Override
    public List<OaRegimeType> selectAll(OaRegimeType oaRegimeType, String startTime, String endTime, Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        return oaRegimeTypeMapper.selectAll(oaRegimeType,startTime,endTime);
    }

    /**
     * 删除所有规则制度类型
     * @param list list里存入id的集合
     * @return 回调函数
     */
    @Override
    public int deleteAll(List<Long> list) {
        return oaRegimeTypeMapper.deleteAll(list);
    }

    /**
     * @param oaRegimeTypeId
     * @param show
     * @return
     */
    @Override
    public List<ThreeVo> loadOaRegimeType(Long oaRegimeTypeId, String show) {

        OaRegimeType regimeType = oaRegimeTypeMapper.selectByPrimaryKey(oaRegimeTypeId);
        List<ThreeVo> threeVos = new ArrayList<>();
        List<OaRegimeType> oaRegimeTypes;
        if(StringUtils.isEmpty(show)){
            oaRegimeTypes = oaRegimeTypeMapper.loadOaRegimeType(oaRegimeTypeId);
        }else{
            oaRegimeTypes = oaRegimeTypeMapper.loadOaRegimeType(null);
        }
        for (OaRegimeType oaRegimeType: oaRegimeTypes) {
            ThreeVo threeVo= new ThreeVo();
            if("1".equals(show)){
                if(regimeType!=null && regimeType.getRegimgTypeSuperior().equals(oaRegimeType.getPkRegimeTypeId())){
                    threeVo.setSelected(true);
                }
            }else if("2".equals(show)){
                if(regimeType!=null && regimeType.getPkRegimeTypeId().equals(oaRegimeType.getPkRegimeTypeId())){
                    threeVo.setSelected(true);
                }
            }
            threeVo.setName(oaRegimeType.getRegimeTypeName());
            threeVo.setValue(oaRegimeType.getPkRegimeTypeId());
            threeVo.setSuperId(oaRegimeType.getRegimgTypeSuperior());
            threeVos.add(threeVo);
        }

        return tools.createTree(threeVos);
    }
}
